﻿package mak.twitterSentiment.classes;
import gr.aueb.cs.nlp.postagger.SmallSetFunctions;
import gr.aueb.cs.nlp.postagger.WordWithCategory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//Class for preprocessing each tweet
//Use of Georgios Ntais Greek stemmer
//Use of AUEB’s Greek part-of-speech (POS) tagger version 2 alpha. 
//This version of the tagger was developed during Evangelia Koleli's BSc thesis 
//in the Department of Informatics, Athens University of Economics and Business
//(AUEB), Greece, 2011
public class Preprocessing {

	private String path;
	private ConnectionToDB c;
	
	public Preprocessing(String path, ConnectionToDB c){
		this.path = path;
		this.c = c;
	}
	
	public Preprocessing(String path){
		this.path = path;
	}
	
	public PreprocessingResult stemWords(String strLine){
		
		String[] results = new String[2];
		try{
			int j;
			List<WordWithCategory> list;
			Map<String,Integer> words = new HashMap<String, Integer>();
	
		  	
		  	String delims_initial = "[ ]+";
		  	int i, valid;
		  	StringBuilder sentence_initial = new StringBuilder();
		  	strLine = strLine.replace(";-)", "<3");
		  	strLine = strLine.replace(":(", "ΓΑΜ");
		  	strLine = strLine.replace(":-(", "ΓΑΜ");
		  	strLine = strLine.replace("\n", "");
		  	strLine = strLine.replace("\"", "");
		  	strLine = strLine.replace("?", "");
		  	strLine = strLine.replace("'", " ");
		  	strLine = strLine.replace(".", " ");
		  	strLine = strLine.replace(",", " ");
		  	strLine = strLine.replace("!", " ");
		  	strLine = strLine.replace(":)", "<3");
		  	strLine = strLine.replace(":-)", "<3");
		  	strLine = strLine.replace(":D", "<3");
		  	strLine = strLine.replace(";)", "<3");
		  	strLine = strLine.replace("#win", "<3");
		  	strLine = strLine.replace(";", "");
		  	strLine = strLine.replace("...", "");
		  	strLine = strLine.replace("-", "");
		  	strLine = strLine.replace(":", " ");
		  	strLine = strLine.replace("#wrse", "ΓΑΜ");
		  	strLine = strLine.replace("#fail", "ΓΑΜ");
		  	strLine = strLine.replace("#oust", "ΓΑΜ");
			strLine = strLine.replace("#wtf", "ΓΑΜ");
			strLine = strLine.replace("#skoupidi", "ΓΑΜ");
			strLine = strLine.replace("#astadiala", "ΓΑΜ");
			strLine = strLine.replace("#chaos", "ΓΑΜ");
		  	String[] tokens_initial = strLine.split(delims_initial);
		  	List<String> noSpaceBefore_initial = new LinkedList<String>(Arrays.asList(",", ".",";", ":", ")", "}", "]"));
			List<String> noSpaceAfter_initial = new LinkedList<String>(Arrays.asList("(", "[","{", "\"",""));

			valid = 0;
			int repeat;
			boolean repeat_flag = false;
			 for (i = 0; i < tokens_initial.length; i++) {
				 
				//Check if a token is not a space, not a link, not a mention, not a RT, not a hashtag. Then check if its three first letters are "χαχ" and if so replace it with "χαχαχ"
				if((!tokens_initial[i].equals("")) && ((tokens_initial[i].startsWith("htt")) == false) && (tokens_initial[i].indexOf('@') != 0) && ((tokens_initial[i].equals("RT")) == false) && (tokens_initial[i].indexOf('#') != 0)){
					if((tokens_initial[i].length() >= 3)&&(tokens_initial[i].substring(0, 3).equals("χαχ"))){
						tokens_initial[i] = "χαχαχ";
					}
					if((tokens_initial[i].length() >= 4)&&(tokens_initial[i].substring(0, 4).equals("αχαχ"))){
						tokens_initial[i] = "χαχαχ";
					}
					repeat_flag = false;
					
					//Check if a token has repetitive characters at the end
					if((tokens_initial[i].length()>3) && ((tokens_initial[i].charAt(tokens_initial[i].length() - 1)) == (tokens_initial[i].charAt(tokens_initial[i].length() - 2)))){
						repeat = tokens_initial[i].length() - 3;
						while((repeat>0) && (repeat_flag == false)){
							if(tokens_initial[i].charAt(repeat) != (tokens_initial[i].charAt(repeat + 1))){
								repeat_flag = true;
								tokens_initial[i] = tokens_initial[i].substring(0, repeat + 1);
							}
							repeat--;
						}
					}
					if(valid!=0){
						
						//Reconstruct tweet from the remaining tokens
						if (noSpaceBefore_initial.contains(tokens_initial[i])|| noSpaceAfter_initial.contains(tokens_initial[i-1])) {
							sentence_initial.append(tokens_initial[i]);
						}	 
						else {
							sentence_initial.append(" " + tokens_initial[i]);
						}

			          // Assumption that opening double quotes are always followed by matching closing double quotes
			          // This block switches the " to the other set after each occurrence
			          // ie The first double quotes should have no space after, then the 2nd double quotes should have no space before
						if ("\"".equals(tokens_initial[i])) {
							if (noSpaceAfter_initial.contains("\"")) {
								noSpaceAfter_initial.remove("\"");
								noSpaceBefore_initial.add("\"");
							} 
							else {
								noSpaceAfter_initial.add("\"");
								noSpaceBefore_initial.remove("\"");
							}
						}
					}
					else{
						sentence_initial.append(tokens_initial[i]);
			  		
					}
					valid++;
				}
				strLine = sentence_initial.toString();
			  }
			 
		  	ArrayList<String> tokens_stem = new ArrayList<String>();
		  	
		  	//POS tagging of the tweet
		  	SmallSetFunctions small = new SmallSetFunctions(path);
		  	list = SmallSetFunctions.smallSetClassifyString(strLine);
		  		
		  	String delims = "[ .,?!:]+";
		  	int index = 0;
		  	boolean negation = false;
		  	String[] tokens = strLine.split(delims);
		  	StringBuilder sentence = new StringBuilder();
		  	
		  	//Check if any part of the tweet matches a negation pattern
		  	for(i=0; i<tokens.length; i++){
		  		if((tokens[i].indexOf('@') != 0) && ((tokens[i].equals("RT")) == false) && (tokens[i].indexOf('#') != 0)){
		  			if(i<list.size()){
		  				//First negation pattern
		  				if(((tokens[i] + " " + "verb").equals(list.get(i).toString())) || ((tokens[i] + " " + "adjective").equals(list.get(i).toString()))){
		  					if((i-1>=0) && ((tokens[i-1].equals("ΔΕΝ")) || (tokens[i-1].equals("ΔΕ")) || (tokens[i-1].equals("MH")) || (tokens[i-1].equals("MHΝ")) || (tokens[i-1].equals("ΚΑΘΟΛ")) || (tokens[i-1].equals("ΟΧΙ")) || (tokens[i-1].equals("ΟΥΤ")))) {
		  						index=i;
		  						negation = true;
		  					}
		  				}
		  				//Second negation pattern
		  				if((tokens[i] + " " + "verb").equals(list.get(i).toString())){
		  					if((i-1>=0) && ((tokens[i-1].equals("ΔΕΝ")) || (tokens[i-1].equals("ΔΕ")))){
		  						if((i+1<tokens.length)&& ((tokens[i+1] + " " + "adjective").equals(list.get(i+1).toString()))){
		  							index = i + 1;
		  							negation = true;
		  						}
		  					}
		  				}
		  				//Third negation pattern
		  				if((tokens[i] + " " + "adverb").equals(list.get(i).toString())){
		  					if((i-1>=0) && ((tokens[i-1].equals("ΟΧΙ")) || (tokens[i-1].equals("ΟΥΤΕ")))){
		  						if((i+1<tokens.length)&& ((tokens[i+1] + " " + "adjective").equals(list.get(i+1).toString()))){
		  							index = i + 1;
		  							negation = true;
		  						}
		  					}
		  				}
		  				//Fourth negation pattern
		  				if((tokens[i] + " " + "adjective").equals(list.get(i).toString())){
		  					if((i-1>=0) && ((tokens[i-1].equals("ΟΧΙ")) || (tokens[i-1].equals("ΟΥΤΕ")))){
		  							index = i;
		  							negation = true;
		  					}
		  				}
		  				//Fifth negation pattern
		  				if((tokens[i] + " " + "verb").equals(list.get(i).toString())){
		  					if((i-1>=0) && ((tokens[i-1].equals("ΔΕΝ")) || (tokens[i-1].equals("ΔΕ")))){
		  						if((i+1<tokens.length)&& ((tokens[i+1] + " " + "adverb").equals(list.get(i+1).toString()))){
		  							index = i + 1;
		  							negation = true;
		  						}
		  					}
		  				}
		  				//Sixth negation pattern
		  				if((tokens[i] + " " + "verb").equals(list.get(i).toString())){
		  					if((i-2>=0) && ((tokens[i-2].equals("ΔΕΝ")) || (tokens[i-2].equals("ΔΕ")))){
		  						if((i-1>=0)&& ((list.get(i-1).getCategory().equals("pronoun")))){
		  							index = i;
		  							negation = true;
		  						}
		  					}
		  				}
		  			}
		  			
		  			//Convert every token to uppercase
		  			tokens[i] = tokens[i].toUpperCase();
		  			
		  			//Find the stem of every token
		  			String word_stemmed = stemWord(tokens[i]);
		  			
		  			//Check if a token tagged with a specific part of speech exists in the database. If so, get the correct polarity 
		  			String polarity = c.executeQuery(word_stemmed, list.get(i).getCategory(), list.get(i).getWord());
		  			    
		  			if(!polarity.equals("")){
			  			if(polarity.equals("positive")){
			  			    word_stemmed = "<3";
			  			}
			  			else if(polarity.equals("negative")){
			  			   word_stemmed = "ΓΑΜ";
			  			}
			  			else if(polarity.equals("neutral")){
			  			   word_stemmed = "ΤΟ";
			  			}
		  			 }
		  			 tokens_stem.add(word_stemmed);
		  		}
		  }
		  	
		  //Store the negated token
		  if(index!=0){
			  results[1] = tokens_stem.get(index);
		  }
		  else{
			  results[1] = "";
		  }
		  
		  List<String> noSpaceBefore = new LinkedList<String>(Arrays.asList(",", ".",";", ":", ")", "}", "]"));
		  List<String> noSpaceAfter = new LinkedList<String>(Arrays.asList("(", "[","{", "\"",""));
		  
		  //Reconstruct tweet with the uppercase stemmed tokens
		  for (i = 0; i < tokens_stem.size(); i++) {
		  	if(i!=0){
		  		if (noSpaceBefore.contains(tokens_stem.get(i))|| noSpaceAfter.contains(tokens_stem.get(i-1))) {
		  			sentence.append(tokens_stem.get(i));
		  		}	 
		  		else {
		  			sentence.append(" " + tokens_stem.get(i));
		  		}

		          // Assumption that opening double quotes are always followed by matching closing double quotes
		          // This block switches the " to the other set after each occurrence
		          // ie The first double quotes should have no space after, then the 2nd double quotes should have no space before
		  		if ("\"".equals(tokens_stem.get(i))) {
		  			if (noSpaceAfter.contains("\"")) {
		  				noSpaceAfter.remove("\"");
		  				noSpaceBefore.add("\"");
		  			} 
		  			else {
		  				noSpaceAfter.add("\"");
		  				noSpaceBefore.remove("\"");
		  			}
		  		}
		  	}
		  	else{
		  		sentence.append(tokens_stem.get(i));
		  	}
		  }
		
		  PreprocessingResult result = new PreprocessingResult();
		  results[0] = sentence.toString();
		  result.setSentence(results[0]);		//Store text of the tweet
		  result.setNegation(results[1]);		//Store if there is negation in the tweet
		  result.setPos(list);					//Store parts of speech for the tokens
		  result.setStems(tokens_stem);			//Store stemmed tokens
		  return result;
	}
	catch(Exception e){
		System.out.println(e);
			return null;
			
	}
		

    
}

//Conversion of Georgios Ntais Greek stemmer to Java	
public String stemWord(String word){
	Map<String,String> step1list=new HashMap<String, String>();

    // adding or set elements in Map by put method key and value pair
	
	step1list.put("ΦΑΓΙΑ", "ΦΑ");
	step1list.put("ΦΑΓΙΟΥ","ΦΑ");
	step1list.put("ΦΑΓΙΩΝ","ΦΑ");
	step1list.put("ΣΚΑΓΙΑ","ΣΚΑ");
	step1list.put("ΣΚΑΓΙΟΥ","ΣΚΑ");
	step1list.put("ΣΚΑΓΙΩΝ","ΣΚΑ");
	step1list.put("ΟΛΟΓΙΟΥ","ΟΛΟ");
	step1list.put("ΟΛΟΓΙΑ","ΟΛΟ");
	step1list.put("ΟΛΟΓΙΩΝ","ΟΛΟ");
	step1list.put("ΣΟΓΙΟΥ","ΣΟ");
	step1list.put("ΣΟΓΙΑ","ΣΟ");
	step1list.put("ΣΟΓΙΩΝ","ΣΟ");
	step1list.put("ΤΑΤΟΓΙΑ","ΤΑΤΟ");
	step1list.put("ΤΑΤΟΓΙΟΥ","ΤΑΤΟ");
	step1list.put("ΤΑΤΟΓΙΩΝ","ΤΑΤΟ");
	step1list.put("ΚΡΕΑΣ","ΚΡΕ");
	step1list.put("ΚΡΕΑΤΟΣ","ΚΡΕ");
	step1list.put("ΚΡΕΑΤΑ","ΚΡΕ");
	step1list.put("ΚΡΕΑΤΩΝ","ΚΡΕ");
	step1list.put("ΠΕΡΑΣ","ΠΕΡ");
	step1list.put("ΠΕΡΑΤΟΣ","ΠΕΡ");
	step1list.put("ΠΕΡΑΤΑ","ΠΕΡ");
	step1list.put("ΠΕΡΑΤΩΝ","ΠΕΡ");
	step1list.put("ΤΕΡΑΣ","ΤΕΡ");
	step1list.put("ΤΕΡΑΤΟΣ","ΤΕΡ");
	step1list.put("ΤΕΡΑΤΑ","ΤΕΡ");
	step1list.put("ΤΕΡΑΤΩΝ","ΤΕΡ");
	step1list.put("ΦΩΣ","ΦΩ");
	step1list.put("ΦΩΤΟΣ","ΦΩ");
	step1list.put("ΦΩΤΑ","ΦΩ");
	step1list.put("ΦΩΤΩΝ","ΦΩ");
	step1list.put("ΚΑΘΕΣΤΩΣ","ΚΑΘΕΣΤ");
	step1list.put("ΚΑΘΕΣΤΩΤΟΣ","ΚΑΘΕΣΤ");
	step1list.put("ΚΑΘΕΣΤΩΤΑ","ΚΑΘΕΣΤ");
	step1list.put("ΚΑΘΕΣΤΩΤΩΝ","ΚΑΘΕΣΤ");
	step1list.put("ΓΕΓΟΝΟΣ","ΓΕΓΟΝ");
	step1list.put("ΓΕΓΟΝΟΤΟΣ","ΓΕΓΟΝ");
	step1list.put("ΓΕΓΟΝΟΤΑ","ΓΕΓΟΝ");
	step1list.put("ΓΕΓΟΝΟΤΩΝ","ΓΕΓΟΝ");
	
	String v = "[ΑΕΗΙΟΥΩ]";
	String v2 = "[ΑΕΗΙΟΩ]";
	
	String w="ΕΞΑΙΡΕΤΙΚΗ";
	String stem="";
	String suffix="";
	String firstch;
	String origword = w;
	String fp1, fp2;
	boolean test1 = true;
	
	w = word;
	w = w.replace('Ή','Η').replace('Ό', 'Ο').replace('Ί', 'Ι').replace('Ά','Α').replace('Ώ', 'Ω').replace('Έ', 'Ε').replace('Ύ', 'Υ');
	test1 = true;
	v = "[ΑΕΗΙΟΥΩ]";
	v2 = "[ΑΕΗΙΟΩ]"; 
	stem="";
	suffix="";
	firstch="";
	origword = w;
	fp1 = ""; 
	fp2 = "";
	
	if (w.length() < 4) { 
	   w = w; 
	}
	else {

	    String re = "(.*)(ΦΑΓΙΑ|ΦΑΓΙΟΥ|ΦΑΓΙΩΝ|ΣΚΑΓΙΑ|ΣΚΑΓΙΟΥ|ΣΚΑΓΙΩΝ|ΟΛΟΓΙΟΥ|ΟΛΟΓΙΑ|ΟΛΟΓΙΩΝ|ΣΟΓΙΟΥ|ΣΟΓΙΑ|ΣΟΓΙΩΝ|ΤΑΤΟΓΙΑ|ΤΑΤΟΓΙΟΥ|ΤΑΤΟΓΙΩΝ|ΚΡΕΑΣ|ΚΡΕΑΤΟΣ|ΚΡΕΑΤΑ|ΚΡΕΑΤΩΝ|ΠΕΡΑΣ|ΠΕΡΑΤΟΣ|ΠΕΡΑΤΑ|ΠΕΡΑΤΩΝ|ΤΕΡΑΣ|ΤΕΡΑΤΟΣ|ΤΕΡΑΤΑ|ΤΕΡΑΤΩΝ|ΦΩΣ|ΦΩΤΟΣ|ΦΩΤΑ|ΦΩΤΩΝ|ΚΑΘΕΣΤΩΣ|ΚΑΘΕΣΤΩΤΟΣ|ΚΑΘΕΣΤΩΤΑ|ΚΑΘΕΣΤΩΤΩΝ|ΓΕΓΟΝΟΣ|ΓΕΓΟΝΟΤΟΣ|ΓΕΓΟΝΟΤΑ|ΓΕΓΟΝΟΤΩΝ)$";
	    Pattern r = Pattern.compile(re);
	    Matcher m = r.matcher(w);
	    if (m.find()) {
	    	fp1 = m.group(1);
	    	fp2 = m.group(2);
	    	stem = fp1;
	    	suffix = fp2;
	    	w = stem + step1list.get(suffix);
	    	test1 = false;
	    }
	
	    re = "^(.+?)(ΑΔΕΣ|ΑΔΩΝ)$";
	    r = Pattern.compile(re);
	    m = r.matcher(w);

	    if (m.find()) {
	    	fp1 = m.group(1);
	    	stem = fp1;
	    	w = stem;

	    	String reg1 = "(ΟΚ|ΜΑΜ|ΜΑΝ|ΜΠΑΜΠ|ΠΑΤΕΡ|ΓΙΑΓΙ|ΝΤΑΝΤ|ΚΥΡ|ΘΕΙ|ΠΕΘΕΡ)$";
	    	Pattern r1 = Pattern.compile(reg1);
	    	Matcher m1 = r1.matcher(w);
	    	if (!(m1.find())) {
	    		w = w + "ΑΔ";
	    	}

	    }
	
	    String re2 = "^(.+?)(ΕΔΕΣ|ΕΔΩΝ)$";
	    Pattern r2 = Pattern.compile(re2);
	    Matcher m2 = r2.matcher(w);
	    if (m2.find()) {
	    	fp1 = m2.group(1);
	    	stem = fp1;
	    	w = stem;

	    	String exept2 = "(ΟΠ|ΙΠ|ΕΜΠ|ΥΠ|ΓΗΠ|ΔΑΠ|ΚΡΑΣΠ|ΜΙΛ)$";
			Pattern r_except2 = Pattern.compile(exept2);
			Matcher m_except2 = r_except2.matcher(w);
			if (m_except2.find()) {
				w = w + "ΕΔ";
			}

	    }
	
	    String re3 = "^(.+?)(ΟΥΔΕΣ|ΟΥΔΩΝ)$";
	    Pattern r3 = Pattern.compile(re3);
	    Matcher m3 = r3.matcher(w);
	    if (m3.find()) {
	    	fp1 = m3.group(1);
	    	stem = fp1;
			w = stem;

			String exept3 = "(ΑΡΚ|ΚΑΛΙΑΚ|ΠΕΤΑΛ|ΛΙΧ|ΠΛΕΞ|ΣΚ|Σ|ΦΛ|ΦΡ|ΒΕΛ|ΛΟΥΛ|ΧΝ|ΣΠ|ΤΡΑΓ|ΦΕ)$";
			Pattern r_except3 = Pattern.compile(exept3);
			Matcher m_except3 = r_except3.matcher(w);
			if (m_except3.find()) {
				w = w + "ΟΥΔ";
			}

	    }
	
	    String re4 = "^(.+?)(ΕΩΣ|ΕΩΝ)$";
	    Pattern r4 = Pattern.compile(re4);
	    Matcher m4 = r4.matcher(w);
	    if (m4.find()) {
	    	fp1 = m4.group(1);
	    	stem = fp1;
	    	w = stem;
	    	test1 = false;
	
	    	String exept4 = "^(Θ|Δ|ΕΛ|ΓΑΛ|Ν|Π|ΙΔ|ΠΑΡ)$";
	    	Pattern r_except4 = Pattern.compile(exept4);
	    	Matcher m_except4 = r_except4.matcher(w);
	    	if (m_except4.find()) {
	    		w = w + "Ε";
	    	}
	    }
	
	    re = "^(.+?)(ΙΑ|ΙΟΥ|ΙΩΝ)$";
	    r = Pattern.compile(re);
	    m = r.matcher(w);
	    if (m.find()) {
	    	fp1 = m.group(1);
	    	stem = fp1;
	    	w = stem;		
	    	re2 = v+"$";
	    	r2 = Pattern.compile(re2);
	    	m2 = r2.matcher(w);
	    	test1 = false;
		
	    	if (m2.find()) {
	    		w = stem + "Ι";	
	    	}
		
	    }
	
	    re = "^(.+?)(ΙΚΑ|ΙΚΟ|ΙΚΟΥ|ΙΚΩΝ)$";
	    r = Pattern.compile(re);
	    m = r.matcher(w);
	    if (m.find()) {
	    	fp1 = m.group(1);
	    	stem = fp1;
	    	w = stem;
	    	test1 = false;
			
	    	re2 = v+"$";
	    	r2 = Pattern.compile(re2);
	    	m2 = r2.matcher(w);
	    	String exept5 = "^(ΑΛ|ΑΔ|ΕΝΔ|ΑΜΑΝ|ΑΜΜΟΧΑΛ|ΗΘ|ΑΝΗΘ|ΑΝΤΙΔ|ΦΥΣ|ΒΡΩΜ|ΓΕΡ|ΕΞΩΔ|ΚΑΛΠ|ΚΑΛΛΙΝ|ΚΑΤΑΔ|ΜΟΥΛ|ΜΠΑΝ|ΜΠΑΓΙΑΤ|ΜΠΟΛ|ΜΠΟΣ|ΝΙΤ|ΞΙΚ|ΣΥΝΟΜΗΛ|ΠΕΤΣ|ΠΙΤΣ|ΠΙΚΑΝΤ|ΠΛΙΑΤΣ|ΠΟΣΤΕΛΝ|ΠΡΩΤΟΔ|ΣΕΡΤ|ΣΥΝΑΔ|ΤΣΑΜ|ΥΠΟΔ|ΦΙΛΟΝ|ΦΥΛΟΔ|ΧΑΣ)$";
	    	Pattern r_except5 = Pattern.compile(exept5);
	    	Matcher m_except5 = r_except5.matcher(w);
	    	if ((m_except5.find()) || (m2.find())){
	    		w = w + "ΙΚ";
	    	}
	    }
	
	    re = "^(.+?)(ΑΜΕ)$";
	    r = Pattern.compile(re);
	    m = r.matcher(w);
	
	    re2 = "^(.+?)(ΑΓΑΜΕ|ΗΣΑΜΕ|ΟΥΣΑΜΕ|ΗΚΑΜΕ|ΗΘΗΚΑΜΕ)$";
	    r2 = Pattern.compile(re2);
	    m2 = r2.matcher(w);
	    if (w == "ΑΓΑΜΕ"){
	    	w = "ΑΓΑΜ";
	    }

	    if (m2.find()) {
	    	fp1 = m2.group(1);
	    	stem = fp1;
	    	w = stem;		
			test1 = false;
	    }

	    if (m.find()) {
	    	fp1 = m.group(1);
			stem = fp1;
			w = stem;
			test1 = false;

			String exept6 = "^(ΑΝΑΠ|ΑΠΟΘ|ΑΠΟΚ|ΑΠΟΣΤ|ΒΟΥΒ|ΞΕΘ|ΟΥΛ|ΠΕΘ|ΠΙΚΡ|ΠΟΤ|ΣΙΧ|Χ)$";
			Pattern r_except6 = Pattern.compile(exept6);
			Matcher m_except6 = r_except6.matcher(w);
			if (m_except6.find()){
				w = w + "ΑΜ";
			}
	    }	

	    re2 = "^(.+?)(ΑΝΕ)$";
	    r2 = Pattern.compile(re2);
	    m2 = r2.matcher(w);
	
	    re3 = "^(.+?)(ΑΓΑΝΕ|ΗΣΑΝΕ|ΟΥΣΑΝΕ|ΙΟΝΤΑΝΕ|ΙΟΤΑΝΕ|ΙΟΥΝΤΑΝΕ|ΟΝΤΑΝΕ|ΟΤΑΝΕ|ΟΥΝΤΑΝΕ|ΗΚΑΝΕ|ΗΘΗΚΑΝΕ)$";
	    r3 = Pattern.compile(re3);
	    m3 = r3.matcher(w);

	    if (m3.find()) {
			fp1 = m3.group(1);
			stem = fp1;
			w = stem;
			test1 = false;
	
			re3 = "^(ΤΡ|ΤΣ)$";		
			r3 = Pattern.compile(re3);
			m3 = r3.matcher(w);

			if (m3.find()){
				w = w + "ΑΓΑΝ";
			}
	    }
	
	    if (m2.find()) {
	    	fp1 = m2.group(1);
	    	stem = fp1;
	    	w = stem;
	    	test1 = false;
			
	    	re2 = v2 +"$";
	    	r2 = Pattern.compile(re2);
	    	m2 = r2.matcher(w);
		
	    	String exept7 = "^(ΒΕΤΕΡ|ΒΟΥΛΚ|ΒΡΑΧΜ|Γ|ΔΡΑΔΟΥΜ|Θ|ΚΑΛΠΟΥΖ|ΚΑΣΤΕΛ|ΚΟΡΜΟΡ|ΛΑΟΠΛ|ΜΩΑΜΕΘ|Μ|ΜΟΥΣΟΥΛΜ|Ν|ΟΥΛ|Π|ΠΕΛΕΚ|ΠΛ|ΠΟΛΙΣ|ΠΟΡΤΟΛ|ΣΑΡΑΚΑΤΣ|ΣΟΥΛΤ|ΤΣΑΡΛΑΤ|ΟΡΦ|ΤΣΙΓΓ|ΤΣΟΠ|ΦΩΤΟΣΤΕΦ|Χ|ΨΥΧΟΠΛ|ΑΓ|ΟΡΦ|ΓΑΛ|ΓΕΡ|ΔΕΚ|ΔΙΠΛ|ΑΜΕΡΙΚΑΝ|ΟΥΡ|ΠΙΘ|ΠΟΥΡΙΤ|Σ|ΖΩΝΤ|ΙΚ|ΚΑΣΤ|ΚΟΠ|ΛΙΧ|ΛΟΥΘΗΡ|ΜΑΙΝΤ|ΜΕΛ|ΣΙΓ|ΣΠ|ΣΤΕΓ|ΤΡΑΓ|ΤΣΑΓ|Φ|ΕΡ|ΑΔΑΠ|ΑΘΙΓΓ|ΑΜΗΧ|ΑΝΙΚ|ΑΝΟΡΓ|ΑΠΗΓ|ΑΠΙΘ|ΑΤΣΙΓΓ|ΒΑΣ|ΒΑΣΚ|ΒΑΘΥΓΑΛ|ΒΙΟΜΗΧ|ΒΡΑΧΥΚ|ΔΙΑΤ|ΔΙΑΦ|ΕΝΟΡΓ|ΘΥΣ|ΚΑΠΝΟΒΙΟΜΗΧ|ΚΑΤΑΓΑΛ|ΚΛΙΒ|ΚΟΙΛΑΡΦ|ΛΙΒ|ΜΕΓΛΟΒΙΟΜΗΧ|ΜΙΚΡΟΒΙΟΜΗΧ|ΝΤΑΒ|ΞΗΡΟΚΛΙΒ|ΟΛΙΓΟΔΑΜ|ΟΛΟΓΑΛ|ΠΕΝΤΑΡΦ|ΠΕΡΗΦ|ΠΕΡΙΤΡ|ΠΛΑΤ|ΠΟΛΥΔΑΠ|ΠΟΛΥΜΗΧ|ΣΤΕΦ|ΤΑΒ|ΤΕΤ|ΥΠΕΡΗΦ|ΥΠΟΚΟΠ|ΧΑΜΗΛΟΔΑΠ|ΨΗΛΟΤΑΒ)$";
	    	Pattern r_except7 = Pattern.compile(exept7);
	    	Matcher m_except7 = r_except7.matcher(w);
	    	if ((m2.find()) || (m_except7.find())){
	    		w = w + "ΑΝ";
	    	}
	    }
	
	    re3 = "^(.+?)(ΕΤΕ)$";
	    r3 = Pattern.compile(re3);
	    m3 = r3.matcher(w);
	
	    re4 = "^(.+?)(ΗΣΕΤΕ)$";
	    r4 = Pattern.compile(re4);
		m4 = r4.matcher(w);

		if (m4.find()) {
			fp1 = m4.group(1);
			stem = fp1;
			w = stem;
			test1 = false;
		}	
	
		if (m3.find()) {
			fp1 = m3.group(1);
			stem = fp1;
			w = stem;
			test1 = false;
				
			re3 = v2 +"$";
			r3 = Pattern.compile(re3);
			m3 = r3.matcher(w);
			String exept8 = "(ΟΔ|ΑΙΡ|ΦΟΡ|ΤΑΘ|ΔΙΑΘ|ΣΧ|ΕΝΔ|ΕΥΡ|ΤΙΘ|ΥΠΕΡΘ|ΡΑΘ|ΕΝΘ|ΡΟΘ|ΣΘ|ΠΥΡ|ΑΙΝ|ΣΥΝΔ|ΣΥΝ|ΣΥΝΘ|ΧΩΡ|ΠΟΝ|ΒΡ|ΚΑΘ|ΕΥΘ|ΕΚΘ|ΝΕΤ|ΡΟΝ|ΑΡΚ|ΒΑΡ|ΒΟΛ|ΩΦΕΛ)$";
			Pattern r_except8 = Pattern.compile(exept8);
			Matcher m_except8 = r_except8.matcher(w);
			
			String exept9 = "^(ΑΒΑΡ|ΒΕΝ|ΕΝΑΡ|ΑΒΡ|ΑΔ|ΑΘ|ΑΝ|ΑΠΛ|ΒΑΡΟΝ|ΝΤΡ|ΣΚ|ΚΟΠ|ΜΠΟΡ|ΝΙΦ|ΠΑΓ|ΠΑΡΑΚΑΛ|ΣΕΡΠ|ΣΚΕΛ|ΣΥΡΦ|ΤΟΚ|Υ|Δ|ΕΜ|ΘΑΡΡ|Θ)$";
			Pattern r_except9 = Pattern.compile(exept9);
			Matcher m_except9 = r_except9.matcher(w);
			if ((m3.find()) || (m_except8.find()) || (m_except9.find())){
				w = w + "ΕΤ";
			}
		}
	
		re = "^(.+?)(ΟΝΤΑΣ|ΩΝΤΑΣ)$";
		r = Pattern.compile(re);
		m = r.matcher(w);
		
		if (m.find()) {
			fp1 = m.group(1);
			stem = fp1;
			w = stem;
			test1 = false;
						
			String exept10 = "^(ΑΡΧ)$";
			Pattern r_except10 = Pattern.compile(exept10);
			Matcher m_except10 = r_except10.matcher(w);
			
			String exept11 = "(ΚΡΕ)$";
			Pattern r_except11 = Pattern.compile(exept11);
			Matcher m_except11 = r_except11.matcher(w);
			if (m_except10.find()){
				w = w + "ΟΝΤ";
			}
			if (m_except11.find()){
				w = w + "ΩΝΤ";
			}
		}
	
		re = "^(.+?)(ΟΜΑΣΤΕ|ΙΟΜΑΣΤΕ)$";
		r = Pattern.compile(re);
		m = r.matcher(w);
		
		if (m.find()) {
			fp1 = m.group(1);
			stem = fp1;
			w = stem;
			test1 = false;
						
			String exept11 = "^(ΟΝ)$";
			Pattern r_except11 = Pattern.compile(exept11);
			Matcher m_except11 = r_except11.matcher(w);
			
			if (m_except11.find()){
				w = w + "ΟΜΑΣΤ";
			}
		}
	
		re = "^(.+?)(ΕΣΤΕ)$";
		r = Pattern.compile(re);
		m = r.matcher(w);
		
		re2 = "^(.+?)(ΙΕΣΤΕ)$";
		r2 = Pattern.compile(re2);
		m2 = r.matcher(w);
		
		if (m2.find()) {
			fp1 = m2.group(1);
			stem = fp1;
			w = stem;
			test1 = false;
					
			re2 = "^(Π|ΑΠ|ΣΥΜΠ|ΑΣΥΜΠ|ΑΚΑΤΑΠ|ΑΜΕΤΑΜΦ)$";
			r2 = Pattern.compile(re2);
			m2 = r2.matcher(w);
		
			if (m2.find()){
				w = w + "ΙΕΣΤ";
			}
		}	
		
		if (m.find()) {
			fp1 = m.group(1);
			stem = fp1;
			w = stem;
			test1 = false;
					
			String exept12 = "^(ΑΛ|ΑΡ|ΕΚΤΕΛ|Ζ|Μ|Ξ|ΠΑΡΑΚΑΛ|ΑΡ|ΠΡΟ|ΝΙΣ)$";
			Pattern r_except12 = Pattern.compile(exept12);
			Matcher m_except12 = r_except12.matcher(w);
			if (m_except12.find()){
				w = w + "ΕΣΤ";
			}
		}
	
		re = "^(.+?)(ΗΚΑ|ΗΚΕΣ|ΗΚΕ)$";
		r = Pattern.compile(re);
		m = r.matcher(w);
		
		re2 = "^(.+?)(ΗΘΗΚΑ|ΗΘΗΚΕΣ|ΗΘΗΚΕ)$";
		r2 = Pattern.compile(re2);
		m2 = r2.matcher(w);
	
		if (m2.find()){
			fp1 = m2.group(1);
			stem = fp1;
			w = stem;
			test1 = false;
		}
		
		if (m.find()) {
			fp1 = m.group(1);
			stem = fp1;
			w = stem;
			test1 = false;
					
			String exept13 = "(ΣΚΩΛ|ΣΚΟΥΛ|ΝΑΡΘ|ΣΦ|ΟΘ|ΠΙΘ)$";
			Pattern r_except13 = Pattern.compile(exept13);
			Matcher m_except13 = r_except13.matcher(w);
			
			String exept14 = "^(ΔΙΑΘ|Θ|ΠΑΡΑΚΑΤΑΘ|ΠΡΟΣΘ|ΣΥΝΘ|)$";
			Pattern r_except14 = Pattern.compile(exept14);
			Matcher m_except14 = r_except14.matcher(w);
			if ((m_except13.find()) || (m_except14.find())){
				w = w + "ΗΚ";
			}
		}
		
		re = "^(.+?)(ΟΥΣΑ|ΟΥΣΕΣ|ΟΥΣΕ)$";
		r = Pattern.compile(re);
		m = r.matcher(w);
		
		if (m.find()) {
				fp1 = m.group(1);
				stem = fp1;
				w = stem;
				test1 = false;
						
			String exept15 = "^(ΦΑΡΜΑΚ|ΧΑΔ|ΑΓΚ|ΑΝΑΡΡ|ΒΡΟΜ|ΕΚΛΙΠ|ΛΑΜΠΙΔ|ΛΕΧ|Μ|ΠΑΤ|Ρ|Λ|ΜΕΔ|ΜΕΣΑΖ|ΥΠΟΤΕΙΝ|ΑΜ|ΑΙΘ|ΑΝΗΚ|ΔΕΣΠΟΖ|ΕΝΔΙΑΦΕΡ|ΔΕ|ΔΕΥΤΕΡΕΥ|ΚΑΘΑΡΕΥ|ΠΛΕ|ΤΣΑ)$";
			Pattern r_except15 = Pattern.compile(exept15);
			Matcher m_except15 = r_except15.matcher(w);
			
			String exept16 = "(ΠΟΔΑΡ|ΒΛΕΠ|ΠΑΝΤΑΧ|ΦΡΥΔ|ΜΑΝΤΙΛ|ΜΑΛΛ|ΚΥΜΑΤ|ΛΑΧ|ΛΗΓ|ΦΑΓ|ΟΜ|ΠΡΩΤ)$";
			Pattern r_except16 = Pattern.compile(exept16);
			Matcher m_except16 = r_except16.matcher(w);
			
			if ((m_except15.find()) || (m_except16.find())){
				w = w + "ΟΥΣ";
			}
		}
		
		re = "^(.+?)(ΑΓΑ|ΑΓΕΣ|ΑΓΕ)$";
		r = Pattern.compile(re);
		m = r.matcher(w);
		
		if (m.find()) {
				fp1 = m.group(1);
				stem = fp1;
				w = stem;
				test1 = false;
			
				String exept17 = "^(ΨΟΦ|ΝΑΥΛΟΧ)$";
				Pattern r_except17 = Pattern.compile(exept17);
				Matcher m_except17 = r_except17.matcher(w);
				
				String exept20 = "(ΚΟΛΛ)$";
				Pattern r_except20 = Pattern.compile(exept20);
				Matcher m_except20 = r_except20.matcher(w);
				
				String exept18 = "^(ΑΒΑΣΤ|ΠΟΛΥΦ|ΑΔΗΦ|ΠΑΜΦ|Ρ|ΑΣΠ|ΑΦ|ΑΜΑΛ|ΑΜΑΛΛΙ|ΑΝΥΣΤ|ΑΠΕΡ|ΑΣΠΑΡ|ΑΧΑΡ|ΔΕΡΒΕΝ|ΔΡΟΣΟΠ|ΞΕΦ|ΝΕΟΠ|ΝΟΜΟΤ|ΟΛΟΠ|ΟΜΟΤ|ΠΡΟΣΤ|ΠΡΟΣΩΠΟΠ|ΣΥΜΠ|ΣΥΝΤ|Τ|ΥΠΟΤ|ΧΑΡ|ΑΕΙΠ|ΑΙΜΟΣΤ|ΑΝΥΠ|ΑΠΟΤ|ΑΡΤΙΠ|ΔΙΑΤ|ΕΝ|ΕΠΙΤ|ΚΡΟΚΑΛΟΠ|ΣΙΔΗΡΟΠ|Λ|ΝΑΥ|ΟΥΛΑΜ|ΟΥΡ|Π|ΤΡ|Μ)$";
				Pattern r_except18 = Pattern.compile(exept18);
				Matcher m_except18 = r_except18.matcher(w);
				
				String exept19 = "(ΟΦ|ΠΕΛ|ΧΟΡΤ|ΛΛ|ΣΦ|ΡΠ|ΦΡ|ΠΡ|ΛΟΧ|ΣΜΗΝ)$";
				Pattern r_except19 = Pattern.compile(exept19);
				Matcher m_except19 = r_except19.matcher(w);
			
				if (((m_except18.find()) || (m_except19.find()))  && !((m_except17.find()) || (m_except20.find()))){
					w = w + "ΑΓ";
				}
		}
	
		re = "^(.+?)(ΗΣΕ|ΗΣΟΥ|ΗΣΑ)$";
		r = Pattern.compile(re);
		m = r.matcher(w);
		
		if (m.find()) {
				fp1 = m.group(1);
				stem = fp1;
				w = stem;
				test1 = false;
						
				String exept21 = "^(Ν|ΧΕΡΣΟΝ|ΔΩΔΕΚΑΝ|ΕΡΗΜΟΝ|ΜΕΓΑΛΟΝ|ΕΠΤΑΝ)$";
				Pattern r_except21 = Pattern.compile(exept21);
				Matcher m_except21 = r_except21.matcher(w);
			
				if (m_except21.find()){
					w = w + "ΗΣ";
				}
		}
		
		re = "^(.+?)(ΗΣΤΕ)$";
		r = Pattern.compile(re);
		m = r.matcher(w);
		
		if (m.find()) {
			fp1 = m.group(1);
			stem = fp1;
			w = stem;
			test1 = false;
						
			String exept22 = "^(ΑΣΒ|ΣΒ|ΑΧΡ|ΧΡ|ΑΠΛ|ΑΕΙΜΝ|ΔΥΣΧΡ|ΕΥΧΡ|ΚΟΙΝΟΧΡ|ΠΑΛΙΜΨ)$";
			Pattern r_except22 = Pattern.compile(exept22);
			Matcher m_except22 = r_except22.matcher(w);
			
			if (m_except22.find()){
				w = w + "ΗΣΤ";
			}
		}
	
		re = "^(.+?)(ΟΥΝΕ|ΗΣΟΥΝΕ|ΗΘΟΥΝΕ)$";
		r = Pattern.compile(re);
		m = r.matcher(w);
		
		if (m.find()) {
				fp1 = m.group(1);
				stem = fp1;
				w = stem;
				test1 = false;
						
				String exept23 = "^(Ν|Ρ|ΣΠΙ|ΣΤΡΑΒΟΜΟΥΤΣ|ΚΑΚΟΜΟΥΤΣ|ΕΞΩΝ)$";
				Pattern r_except23 = Pattern.compile(exept23);
				Matcher m_except23 = r_except23.matcher(w);
			
				if (m_except23.find()){
					w = w + "ΟΥΝ";
				}
		}
		
		re = "^(.+?)(ΟΥΜΕ|ΗΣΟΥΜΕ|ΗΘΟΥΜΕ)$";
		r = Pattern.compile(re);
		m = r.matcher(w);
		
		if (m.find()) {
			fp1 = m.group(1);
			stem = fp1;
			w = stem;
			test1 = false;
						
			String exept24 = "^(ΠΑΡΑΣΟΥΣ|Φ|Χ|ΩΡΙΟΠΛ|ΑΖ|ΑΛΛΟΣΟΥΣ|ΑΣΟΥΣ)$";
			Pattern r_except24 = Pattern.compile(exept24);
			Matcher m_except24 = r_except24.matcher(w);
			
			if (m_except24.find()){
				w = w + "ΟΥΜ";
			}
		}
	
		re="^(.+?)(ΜΑΤΑ|ΜΑΤΩΝ|ΜΑΤΟΣ)$";
		r = Pattern.compile(re);
		m = r.matcher(w);
		
		re2 = "^(.+?)(Α|ΑΓΑΤΕ|ΑΓΑΝ|ΑΕΙ|ΑΜΑΙ|ΑΝ|ΑΣ|ΑΣΑΙ|ΑΤΑΙ|ΑΩ|Ε|ΕΙ|ΕΙΣ|ΕΙΤΕ|ΕΣΑΙ|ΕΣ|ΕΤΑΙ|Ι|ΙΕΜΑΙ|ΙΕΜΑΣΤΕ|ΙΕΤΑΙ|ΙΕΣΑΙ|ΙΕΣΑΣΤΕ|ΙΟΜΑΣΤΑΝ|ΙΟΜΟΥΝ|ΙΟΜΟΥΝΑ|ΙΟΝΤΑΝ|ΙΟΝΤΟΥΣΑΝ|ΙΟΣΑΣΤΑΝ|ΙΟΣΑΣΤΕ|ΙΟΣΟΥΝ|ΙΟΣΟΥΝΑ|ΙΟΤΑΝ|ΙΟΥΜΑ|ΙΟΥΜΑΣΤΕ|ΙΟΥΝΤΑΙ|ΙΟΥΝΤΑΝ|Η|ΗΔΕΣ|ΗΔΩΝ|ΗΘΕΙ|ΗΘΕΙΣ|ΗΘΕΙΤΕ|ΗΘΗΚΑΤΕ|ΗΘΗΚΑΝ|ΗΘΟΥΝ|ΗΘΩ|ΗΚΑΤΕ|ΗΚΑΝ|ΗΣ|ΗΣΑΝ|ΗΣΑΤΕ|ΗΣΕΙ|ΗΣΕΣ|ΗΣΟΥΝ|ΗΣΩ|Ο|ΟΙ|ΟΜΑΙ|ΟΜΑΣΤΑΝ|ΟΜΟΥΝ|ΟΜΟΥΝΑ|ΟΝΤΑΙ|ΟΝΤΑΝ|ΟΝΤΟΥΣΑΝ|ΟΣ|ΟΣΑΣΤΑΝ|ΟΣΑΣΤΕ|ΟΣΟΥΝ|ΟΣΟΥΝΑ|ΟΤΑΝ|ΟΥ|ΟΥΜΑΙ|ΟΥΜΑΣΤΕ|ΟΥΝ|ΟΥΝΤΑΙ|ΟΥΝΤΑΝ|ΟΥΣ|ΟΥΣΑΝ|ΟΥΣΑΤΕ|Υ|ΥΣ|Ω|ΩΝ)$";
		r2 = Pattern.compile(re2);
		m2 = r2.matcher(w);
	
		if (m.find()) {
			fp1 = m.group(1);
			stem = fp1;
			w = stem + "ΜΑ";
		}
		if ((m2.find())&&(test1)){
			fp1 = m2.group(1);
			stem = fp1;
			w = stem;
		}
		
		re = "^(.+?)(ΕΣΤΕΡ|ΕΣΤΑΤ|ΟΤΕΡ|ΟΤΑΤ|ΥΤΕΡ|ΥΤΑΤ|ΩΤΕΡ|ΩΤΑΤ)$";
		r = Pattern.compile(re);
		m = r.matcher(w);
	
		if (m.find()){
			fp1 = m.group(1);
			stem = fp1;
			w = stem;
		}
    }
	
	return w;

	}	

}
